package com.example.demo.service;

import com.example.demo.dao.CustomMapper;
import com.example.demo.dao.PrivilegeMapper;
import com.example.demo.entity.Privilege;
import com.example.demo.pojo.BSResult;
import com.example.demo.pojo.ZTreeNode;
import com.example.demo.utils.BSResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class PrivilegeServiceImpl implements PrivilegeService {

    @Autowired
    private PrivilegeMapper privilegeMapper;

    @Autowired
    private CustomMapper customMapper;


    @Override
    public List<ZTreeNode> getZTreeNodes() {
        List<Privilege> privileges = privilegeMapper.selectAll();
        List<ZTreeNode> zTreeNodes = new ArrayList<>();

        privileges.forEach((privilege)->{
            ZTreeNode zTreeNode = new ZTreeNode();
            zTreeNode.setId(privilege.getPriv_id());
            zTreeNode.setName(privilege.getName());
            zTreeNode.setpId(privilege.getParent_id());
            if(zTreeNode.getpId() == 0){
                zTreeNode.setOpen(true);
            }
            zTreeNodes.add(zTreeNode);
        });
        return zTreeNodes;
    }


    @Override
    public BSResult updatePrivilege(Privilege privilege) {
        privilege.setUpdated(new Date());

        privilegeMapper.updateByPrimaryKeySelective(privilege);
        return BSResultUtils.success(privilegeMapper.selectByPrimaryKey(privilege.getPriv_id()));
    }

    @Override
    public Privilege findById(Integer privilegeId) {
        return privilegeMapper.selectByPrimaryKey(privilegeId);
    }

    @Override
    public BSResult addPrivilege(Privilege privilege) {

        privilege.setCreated(new Date());
        privilege.setUpdated(new Date());
        privilegeMapper.insertSelective(privilege);
        Privilege parentPrivilege=privilegeMapper.selectByPrimaryKey(privilege.getParent_id());

        if(parentPrivilege.getIs_parent()==0){
            parentPrivilege.setIs_parent(1);
            parentPrivilege.setUpdated(new Date());
            this.updatePrivilege(parentPrivilege);
        }


        return BSResultUtils.success(privilege);
    }

    @Override
    public BSResult deleteById(Integer privId) {

        try{
            Privilege privilege=privilegeMapper.selectByPrimaryKey(privId);
            privilegeMapper.deleteByPrimaryKey(privId);
            Example example=new Example(Privilege.class);
            Example.Criteria criteria=example.createCriteria();
            criteria.andEqualTo("parent_id",privilege.getParent_id());
            List<Privilege> privileges=privilegeMapper.selectByExample(example);
            if(privileges.size()==0){
                Privilege parentPrivilege=privilegeMapper.selectByPrimaryKey(new Integer(privilege.getParent_id()));
                parentPrivilege.setIs_parent(0);
                this.updatePrivilege(parentPrivilege);
            }
            return  BSResultUtils.success(privId);

        }catch(Exception e){
           return BSResultUtils.build(400, "此权限被其他地方引用,不能删除");
        }

    }


    @Override
    public List<ZTreeNode> findPrivilegeByRoleId(int roleId) {

        List<Privilege> allPrivileges=privilegeMapper.selectAll();
        List<Privilege> roleprivileges= customMapper.findPrivilegeByRoleId(roleId);

        List<ZTreeNode> zTreeNodes=new ArrayList<>();

        allPrivileges.forEach((privilege)->{

        ZTreeNode zTreeNode=new ZTreeNode();
        zTreeNode.setId(privilege.getPriv_id());
        zTreeNode.setName(privilege.getName());
        zTreeNode.setpId(privilege.getParent_id());
        if(privilege.getIs_parent()==1)
            zTreeNode.setOpen(true);

        for(Privilege privilege1:roleprivileges){

        if(privilege1.getPriv_id()==privilege.getPriv_id()){
            zTreeNode.setChecked(true);
            break;
        }

        }

        zTreeNodes.add(zTreeNode);

        });

        return zTreeNodes;
    }
}
